package cn.bjxy.shishaolong.system.controller;


import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.ra_room.entity.Raroom;
import cn.bjxy.shishaolong.ra_room.service.IRaroomService;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import cn.bjxy.shishaolong.system.user.service.ISysUserInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 老师答疑室 前端控制器
 * </p>
 *
 * @author 时少龙
 * @since 2019-04-13
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/admin/rm")
public class RaroomManageController extends AbstractController {

    @Autowired
    private IRaroomService raroomService;

    @Autowired
    private ISysUserInfoService infoService;

    /**
     * 获取开通答疑室所需的所有的数据
     *
     * @return
     */
    /*@RequestMapping("/getAllData")
    @Permission
    public ResultMsg getAllData(HttpServletRequest request) {
        try {
            Integer currentUserId = getCurrentUserId(request);
            SysUserInfo userInfo = infoService.getById(currentUserId);

            // Todo 检查是否已经开启过了


            RaroomQueryEntity allData = raroomService.getAllData();
            allData.setTeacherName(userInfo.getName());
            return ResultMsg.success(allData);
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }*/

    /**
     * 条件查询
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryByCondition")
    @Permission
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            Raroom raroom = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                raroom = param.getObject(SysConst.QUERY_CONDITION_KEY, Raroom.class);
            }else{
                raroom = new Raroom();
            }
            raroom.setIsAdmin(1);
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Raroom> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Raroom> list = raroomService.queryByCondition(page, raroom);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Raroom> list = raroomService.queryByCondition(raroom);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return retQueryFailure();
        }
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    @DeleteMapping("/delete")
    @Permission
    public ResultMsg delete(@RequestBody JSONObject param) {
        try {
            if (param == null || !param.containsKey(SysConst.BATCH_ID)
                    || !param.getString(SysConst.BATCH_ID).contains(",")) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            String ids = param.getString(SysConst.BATCH_ID);
            raroomService.remove(ids);
            return ResultMsg.success();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return ResultMsg.create(ofe.getCode(), ofe.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return retDeleteFailure();
        }
    }

    /**
     * TODO 方法加权限验证
     * 获取开通答疑室所需的所有的数据
     *
     * @return
     */
    /*@RequestMapping("/open")
    @Permission
    public ResultMsg open(HttpServletRequest request, @RequestBody Raroom raroom) {
        try {
            if (raroom == null) {
                retParamFailure();
            }
            Integer currentUserId = getCurrentUserId(request);
            SysUserInfo userInfo = infoService.getById(currentUserId);

            // Todo 检查是否已经开启过了
            raroom.setTeacherId(currentUserId); // 填写老师的id
            raroomService.open(raroom);
            return retSuccess();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.OPERATOR_FAILURE, StatusCode.OPERATOR_FAILURE_MSG);
        }
    }
*/
    /**
     * TODO 方法加权限验证
     * 获取开通答疑室所需的所有的数据
     *
     * @return
     */
  /*  @RequestMapping("/changeRoomStatus")
    @Permission
    public ResultMsg changeRoomStatus(HttpServletRequest request) {
        try {

            Integer currentUserId = getCurrentUserId(request);
            raroomService.changeRoomStatus(currentUserId);
            return retSuccess();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return retOperatorFalueException(ofe);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.OPERATOR_FAILURE, StatusCode.OPERATOR_FAILURE_MSG);
        }
    }*/
}
